﻿using System;
using System.Collections.Generic;
using System.Web;
using DotNetNuke.Entities.Modules;

namespace SE.HTMLRepeater.Components
{
    public static class Util
    {
        public static string getTemplate(PortalModuleBase moduleBase)
        {
            object setting = moduleBase.Settings["template"];
            if (setting != null)
            {
                string settingValue = setting.ToString();
                return settingValue;
            }
            return "";
        }


        public static List<string> GetTemplateItems(string Template)
        {
            List<string> items = new List<string>();
            int iStart = Template.IndexOf("!", 0);
            while (iStart > -1)
            {
                int iStop = Template.IndexOf("!", iStart + 1);
                if (iStop != -1 && iStop - iStart < Template.Length)
                {
                    string varName = Template.Substring(iStart + 1, (iStop - iStart) - 1);
                    items.Add(varName);
                }
                iStart = Template.IndexOf("!", iStop + 1);
            }
            return items;
        }

        public static List<DisplayVar> DecompressVariables(string Vars, List<string> TemplateItems)
        {
            List<DisplayVar> variables = new List<DisplayVar>();

            if (TemplateItems != null)
            {
                //Add all proper template values to insure that they are in the list and also the top items.
                foreach (string s in TemplateItems)
                {
                    DisplayVar displayVar = new DisplayVar(s, "");

                    DisplayVar dV = variables.Find(dv => dv.VarName == displayVar.VarName); //predicate short hand search.

                    if (dV == null)
                    { variables.Add(displayVar); }
                }
            }

            string[] Vs = Vars.Split(new string[] { "`,`" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string v in Vs)
            {
                if (v.Length != 0)
                {
                    DisplayVar dv = null;
                    string[] vs = v.Split(new string[]{"`~`"},StringSplitOptions.RemoveEmptyEntries);
                    if (vs.Length == 2) //must have 2 elements no more no less.
                    {
                        //check to see that it is in the current template. Include only the ones that exist.
                        if (TemplateItems == null || TemplateItems.Contains(vs[0]))
                        {
                            DisplayVar Dv = variables.Find(DVar => DVar.VarName == vs[0]); //predicate short hand search.
                            
                            if (Dv != null)
                            {
                                Dv.VarValue = vs[1];
                            }
                            else
                            {
                                dv = new DisplayVar(vs[0], vs[1]);
                                variables.Add(dv);
                            }

                        }
                        else
                        {
                            //add with special tag to include in old values text area
                            //this is so that when you change the template if you have values you are just moving to a new
                            //variable you can still copy and past them to make the initial update.
                            dv = new DisplayVar("!~" + vs[0], vs[1]);
                            variables.Add(dv);
                        }
                    }
                }
            }

            return variables;

        }

        public static List<DisplayVar> DecompressVariables(string Vars)
        {
            return DecompressVariables(Vars, null);
        }


    }
    public class DisplayVar
    {
        public string VarName { get; set; }
        public string VarValue { get; set; }
        public DisplayVar(string varName, string varValue)
        {
            VarName = varName;
            VarValue = varValue;
        }
    }
}
